package com.duguo.dynamicmvc.test;


import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.easymock.IExpectationSetters;
import org.easymock.classextension.EasyMock;

public abstract class EasyMockTestCase extends TestCase {
	
	private List<Object> mocks;
	
    public <T> T createMock(Class<T> toMock) {
    	T mock=EasyMock.createMock(toMock);
    	addMock(mock);
		return mock;
    }
	
    public <T> T createNiceMock(Class<T> toMock) {
    	T mock=EasyMock.createNiceMock(toMock);
    	addMock(mock);
		return mock;
    }
	
    public IExpectationSetters<Object> expect(Object mock) {
    	return EasyMock.expect(mock);
    }
	
    public void replay() {
    	assertNotNull(mocks);
    	if(mocks!=null) {
			for (Object mock : mocks) {
				EasyMock.replay(mock);
			}
    	}
    }
    
    public void removeMock(Object mock) {
    	assertNotNull(mocks);
    	mocks.remove(mock);
    }

	protected void tearDown() throws Exception {
		if(mocks!=null) {
			for (Object mock : mocks) {
				EasyMock.verify(mock);
			}
		}
		mocks=null;
		super.tearDown();			
	}
	
	private void addMock(Object mock) {
		if(mocks==null) 
			mocks=new ArrayList<Object>();
		mocks.add(mock);
	}
    
}
